package msc.ai.crypto.jade.security;

import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;

import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;

public class TestClass
{
	public static void main(String[] args)
	{
		try
		{
//			KeyGenerator kg = KeyGenerator.getInstance("AES");
//			SecretKey key = kg.generateKey();
//			System.out.println(key);
			
			KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
//			KeyPair kp = kpg.generateKeyPair();
//			System.out.println(kp.getPrivate());
//			System.out.println(kp.getPublic());
			
			KeyPair kp2 = kpg.generateKeyPair();
			kpg.initialize(1024);
//			System.out.println(kp2.getPrivate());
//			System.out.println(kp2.getPublic());
			
			String text = "THIS IS MY TEXT";
//			Cipher cpE = Cipher.getInstance("DES/CBC/PKCS5Paddin");
			
//			Cipher cpE = Cipher.getInstance("RSA");
			Cipher cpE = Cipher.getInstance("RSA/ECB/PKCS1Padding");
			cpE.init(Cipher.ENCRYPT_MODE, kp2.getPrivate());
//			cpE.init(Cipher.ENCRYPT_MODE, kp2.getPublic(), );
			byte[] cipherText = cpE.doFinal( text.getBytes("UTF-8") );
			char[] charE = Hex.encodeHex(cipherText);
			String textE = new String(charE);
			System.out.println(textE);
			
			
			
//			Cipher cpD = Cipher.getInstance("RSA");
			Cipher cpD = Cipher.getInstance("RSA/ECB/PKCS1Padding");
			cpD.init(Cipher.DECRYPT_MODE, kp2.getPublic());			
			byte[] bts = Hex.decodeHex(textE.toCharArray());

			//byte[] decrypted = blockCipher(bts,Cipher.DECRYPT_MODE);
			
			byte[] textD = cpD.doFinal( bts );
            String actualMsgText = new String( textD, "UTF-8" );
            
            System.out.println(actualMsgText);
			
			
			
		} catch (NoSuchAlgorithmException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvalidKeyException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchPaddingException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalBlockSizeException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (BadPaddingException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedEncodingException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (DecoderException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
//		try
//		{
//			KeyStore ks = KeyStore.getInstance("RSA");
//	
//			
//			
//			
//		} catch (KeyStoreException e)
//		{
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		
	}
}
